package com.bbm.bbmanage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bbm.bbmanage.entity.AppHttpCodeEnum;
import com.bbm.bbmanage.entity.HouseProperty;
import com.bbm.bbmanage.entity.Property;
import com.bbm.bbmanage.entity.dto.HousePropertyDTO;
import com.bbm.bbmanage.entity.vo.LabelValueVo;
import com.bbm.bbmanage.entity.vo.ResponseResult;
import com.bbm.bbmanage.mapper.HousePropertyMapper;
import com.bbm.bbmanage.mapper.PropertyMapper;
import com.bbm.bbmanage.service.HousePropertyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * (HouseProperty)表服务实现类
 *
 * @author lionkliu
 * @since 2024-03-23 18:53:08
 */
@Service("housePropertyService")
public class HousePropertyServiceImpl extends ServiceImpl<HousePropertyMapper, HouseProperty> implements HousePropertyService {

    @Autowired
    private PropertyMapper propertyMapper;


    @Override
    public ResponseResult selectPage(Integer pageNum, Integer pageSize) {
        return ResponseResult.okResult(page(new Page<>(pageNum, pageSize)));
    }

    @Override
    public ResponseResult selectOne(Integer id) {
        return ResponseResult.okResult(getById(id));
    }

    @Override
    public ResponseResult insertOne(HouseProperty houseProperty) {
        return save(houseProperty) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.INSERT_ERROR);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        return removeById(id) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.DELETE_ERROR);
    }

    @Override
    public ResponseResult updateOne(HouseProperty houseProperty) {
        UpdateWrapper<HouseProperty> updateWrapper = new UpdateWrapper();
        // TODO：
        return update(updateWrapper) ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_ERROR);
    }

    @Override
    public ResponseResult getPropertiesByHouseId(Long id) {
        LambdaQueryWrapper<HouseProperty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseProperty::getHouseId, id);
        List<HouseProperty> list = list(queryWrapper);
        List<Property> collect = list.stream().map(houseProperty -> {
            Integer propertyId = houseProperty.getPropertyId();
            LambdaQueryWrapper<Property> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Property::getId, propertyId);
            return propertyMapper.selectOne(wrapper);
        }).collect(Collectors.toList());

        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult getPropertiesLabelValueByHouseId(Long houseId) {
        LambdaQueryWrapper<HouseProperty> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HouseProperty::getHouseId, houseId);

        List<LabelValueVo> collect = list(queryWrapper).stream().map((houseProperty -> {
            LabelValueVo labelValueVo = new LabelValueVo();
            labelValueVo.setValue(String.valueOf(houseProperty.getPropertyId()));
            LambdaQueryWrapper<Property> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Property::getId, houseProperty.getPropertyId());
            Property property = propertyMapper.selectOne(wrapper);

            labelValueVo.setLabel(property.getName());
            return labelValueVo;
        })).collect(Collectors.toList());
        return ResponseResult.okResult(collect);
    }

    @Override
    public ResponseResult insert(HousePropertyDTO houseProperty) {
        Integer[] propertyIds = houseProperty.getPropertyIds();
        Arrays.stream(propertyIds).forEach(propertyId -> {
            LambdaQueryWrapper<HouseProperty> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(HouseProperty::getHouseId, houseProperty.getHouseId())
                    .eq(HouseProperty::getPropertyId, propertyId);
            HouseProperty one = getOne(queryWrapper);
            if (Objects.isNull(one)) {
                HouseProperty property = new HouseProperty();
                property.setPropertyId(propertyId)
                        .setHouseId(houseProperty.getHouseId());
                save(property);
            }
        });

        return ResponseResult.okResult();
    }

}

